package cn.itcast.oa.util;

import cn.itcast.oa.domain.Department;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by ChengGM on 2016/12/8.
 */
public class DepartmentUtils {
    /**
     * 遍历部门树，把按序排好信息入到一个集合中返回
     * @param topList
     * @return
     */
    public static List<Department> getAllDepartments(List<Department> topList){
        List list=new ArrayList();
        walkDeparmentTreeList(topList,"┝",list);
        return list;
    }

    /**
     * 遍历部门树，把遍历的部门信息放到指定的集合中
     * @param topList
     * @param prefix
     * @param list
     */
    private static void walkDeparmentTreeList(Collection<Department> topList,String prefix,List<Department> list){
        //Collection是父类接口，list,map,set都继承它
        for(Department top:topList){
            //顶点
            Department copy = new Department();
            copy.setId(top.getId());
            copy.setName(prefix+top.getName());
            list.add(copy);
            //子树
            walkDeparmentTreeList(top.getChildren()," "+prefix,list);
        }
    }
}
